﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XRERP.Models;
using XRERP.utils;

namespace XRERP.Controllers
{
    /// <summary>
    /// 设备管理控
    /// </summary>
    public class DeviceManageController : Controller
    {

        private MySqlDB db = new MySqlDB();

        // GET: DeviceManage
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult AddClassify(String devmain_name)
        {
            xr_bl_devconfigmain info = new xr_bl_devconfigmain();
            JRow<xr_bl_devconfigmain> json = new JRow<xr_bl_devconfigmain>();
            if (devmain_name.Equals(null) || devmain_name == "")
            {
                json.code = 500;
                json.msg = "您什么也没有输入!";
                json.isError = true;
                return Json(json);
            }
            try
            {
                devmain_name = HttpUtility.UrlDecode(devmain_name);
                info.devmain_name = devmain_name;
                info.devmain_uuid = Tools.getGuid();
                info.validflag = "1";
                db.xr_bl_devconfigmain.Add(info);
                if (db.SaveChanges() == 1)
                {
                    json.isError = false;
                    json.code = 200;
                    json.msg = "保存成功!";
                    json.RowData = info;
                }
                else
                {
                    json.isError = true;
                    json.code = 300;
                    json.msg = "保存失败，请重试或与管理员联系！";
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.isError = true;
                json.msg = e.ToString();
            }
            return Json(json);
        }


        public ActionResult EditClassify(String devmain_uuid, String devmain_name)
        {
            xr_bl_devconfigmain info = new xr_bl_devconfigmain();
            JRow<xr_bl_devconfigmain> json = new JRow<xr_bl_devconfigmain>();

            if (devmain_uuid.Equals(null) || devmain_name.Equals(null) || devmain_uuid == "" || devmain_name == "")
            {
                json.code = 500;
                json.isError = false;
                json.msg = "请输入有效值";
                return Json(json);
            }
            try
            {
                devmain_name = HttpUtility.UrlDecode(devmain_name);
                info = db.xr_bl_devconfigmain.FirstOrDefault(x => x.devmain_uuid == devmain_uuid);
                info.devmain_name = devmain_name;
                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "修改成功!";
                    json.isError = false;
                    json.RowData = info;
                }
                else
                {
                    json.code = 300;
                    json.msg = "修改失败，请重试或与管理员联系！";
                    json.isError = true;
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
                json.isError = true;
            }

            return Json(json);
        }

        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="id">要删除的ID</param>
        /// <returns></returns>
        public ActionResult DelClassify(String id)
        {
            xr_bl_devconfigmain info = new xr_bl_devconfigmain();
            JR json = new JR();
            if (id.Equals(null) || id == "")
            {
                json.code = 500;
                json.success = false;
                json.msg = "请输入有效值";
                return Json(json);
            }
            try
            {
                info = db.xr_bl_devconfigmain.FirstOrDefault(x => x.devmain_uuid == id);
                info.validflag = "0";
                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "删除成功!";
                    json.success = false;
                }
                else
                {
                    json.code = 300;
                    json.msg = "删除失败，请重试或与管理员联系！";
                    json.success = true;
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
                json.isError = true;
            }

            return Json(json);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="customeruser">要查询的客户名称</param>
        /// <param name="page">页面</param>
        /// <param name="rows">行数</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult QueryDeviceMan(int page = 1, int rows = 10)
        {
            Euitable<xr_bl_devconfigmain> table = new Euitable<xr_bl_devconfigmain>();
            //设置查询条件
            Func<xr_bl_devconfigmain, bool> where = x => x.validflag == "1";
            table.rows = db.xr_bl_devconfigmain
                .Where(where)
                .OrderBy(y => y.devmain_uuid)
                .Skip<xr_bl_devconfigmain>(rows * (page - 1))
                .Take<xr_bl_devconfigmain>(rows)
                .ToList<xr_bl_devconfigmain>();
            table.total = db.xr_bl_devconfigmain
                .Where(where)
                .Count();

            return Json(table);
        }


        [HttpPost]
        public ActionResult AddDevice(String devsub_name,String devmain_uuid, String price1, String price2 , String price3 )
        {
            xr_bl_devconfigsub info = new xr_bl_devconfigsub();
            JRow<xr_bl_devconfigsub> json = new JRow<xr_bl_devconfigsub>();
            if (devsub_name.Equals(null) || devsub_name == "")
            {
                json.code = 500;
                json.msg = "请输入配件名称!";
                json.isError = true;
                return Json(json);
            }

            if (devmain_uuid.Equals(null) || devmain_uuid == "")
            {
                json.code = 500;
                json.msg = "未传送设备分类ID!";
                json.isError = true;
                return Json(json);
            }

            if (price1.Equals(null)|| price1=="")
            {
                json.code = 500;
                json.msg = "请输入包邮单价!";
                json.isError = true;
                return Json(json);
            }


            try
            {
                info.devmain_uuid = devmain_uuid;
                info.devsub_uuid = Tools.getGuid();
                info.devsub_name = devsub_name;
                info.validflag = "1";
                info.price1 = Decimal.Parse(price1);

                if (!price2.Equals(null) && price2.Trim() != "")
                {
                    info.price2 = Decimal.Parse(price2);
                }

                if (!price3.Equals(null) && price3.Trim() != "")
                {
                    info.price3 = Decimal.Parse(price3);
                }

                db.xr_bl_devconfigsub.Add(info);
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "保存成功!";
                    json.RowData = info;
                }
                else
                {
                    json.code = 300;
                    json.msg = "保存失败，请重试或与管理员联系！";

                }
            }
            catch(Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
                json.isError = true;
            }


            return Json(json);
        }

        [HttpPost]
        public ActionResult EditDevice(String devsub_name, String devsub_uuid, String price1, String price2, String price3)
        {
            xr_bl_devconfigsub info = new xr_bl_devconfigsub();
            JRow<xr_bl_devconfigsub> json = new JRow<xr_bl_devconfigsub>();

            if (devsub_uuid.Equals(null) || devsub_name.Equals(null) || devsub_uuid == "" || devsub_uuid == "" || price1.Equals(null) || price1 == "")
            {
                json.code = 500;
                json.isError = false;
                json.msg = "请输入有效值";
                return Json(json);
            }
            try
            {
                info = db.xr_bl_devconfigsub.FirstOrDefault(x => x.devsub_uuid == devsub_uuid);
                info.devsub_name = devsub_name;
                info.price1 = Decimal.Parse(price1);
                
                if(!price2.Equals(null) && price2.Trim() !="")
                {
                    info.price2 = Decimal.Parse(price2);
                }

                if(!price3.Equals(null) && price3.Trim() != "")
                {
                    info.price3 = Decimal.Parse(price3);
                }

                db.Entry(info).State = EntityState.Modified;
                if (db.SaveChanges() == 1)
                {
                    json.code = 200;
                    json.msg = "修改成功!";
                    json.isError = false;
                    json.RowData = info;
                }
                else
                {
                    json.code = 300;
                    json.msg = "修改失败，请重试或与管理员联系！";
                    json.isError = true;
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = "'"+ price3 + "'"+e.ToString();
                json.isError = true;
            }

            return Json(json);
        }


       /// <summary>
       /// 删除设备信息
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        public ActionResult DelDevice(String id)
        {
            xr_bl_devconfigsub info = new xr_bl_devconfigsub();
            JR json = new JR();
            if (id.Equals(null) || id == "")
            {
                json.code = 500;
                json.success = false;
                json.msg = "请输入有效值";
                return Json(json);
            }
            try
            {
                info = db.xr_bl_devconfigsub.FirstOrDefault(x => x.devsub_uuid == id);
                if (info.Equals(null))
                {
                    info.validflag = "0";
                    db.Entry(info).State = EntityState.Modified;
                    if (db.SaveChanges() == 1)
                    {
                        json.code = 200;
                        json.msg = "删除成功!";
                        json.success = false;
                    }
                    else
                    {
                        json.code = 300;
                        json.msg = "删除失败，请重试或与管理员联系！";
                        json.success = true;
                    }
                }
                else
                {
                    json.code = 300;
                    json.msg = "删除失败,该条信息不存在，或已被删除！";
                    json.success = true;
                }
            }
            catch (Exception e)
            {
                json.code = 505;
                json.msg = e.ToString();
                json.isError = true;
            }

            return Json(json);
        }

        [HttpPost]
        public ActionResult QueryDevice(int page = 1, int rows = 10, String devmain_uuid="")
        {
            Euitable<xr_bl_devconfigsub> table = new Euitable<xr_bl_devconfigsub>();
            if (devmain_uuid != "")
            {
                Func<xr_bl_devconfigsub, bool> where = x => x.validflag == "1";
                Func<xr_bl_devconfigsub, bool> where2 = x => x.devmain_uuid == devmain_uuid;

                table.rows = db.xr_bl_devconfigsub
                    .Where(where)
                    .Where(where2)
                    .OrderBy(y => y.devmain_uuid)
                    .Skip<xr_bl_devconfigsub>(rows * (page - 1))
                    .Take<xr_bl_devconfigsub>(rows)
                    .ToList<xr_bl_devconfigsub>();
                table.total = db.xr_bl_devconfigsub
                    .Where(where)
                    .Where(where2)
                    .Count();

            }
            else {
                table.total=0;
                table.rows = new List<xr_bl_devconfigsub>();
                    }
             return Json(table);

        }
    }
}